import 'package:dio/dio.dart';
import 'package:fsharing/core/models/cart_list.dart';
import 'package:fsharing/core/models/product_detail.dart';
import 'package:fsharing/core/services/api.dart';
import 'package:fsharing/core/viewmodels/view_model.dart';

class OrderCheckModel extends ViewModel {
  final Api api;
  final Map<int, bool> _checkMap = {};
  Map<String, int> _cartIdQuantity;

  String _errorMessage;

  OrderCheckModel(this.api) : super();

  String get errorMessage => _errorMessage;

  Map<String, int> get cartIdQuantity => _cartIdQuantity;

  bool getCheckAll(CartList cartList) {
    if (cartList.carts.isEmpty) {
      return false;
    }
    for (Store store in cartList.carts) {
      if (!getCheckStore(store)) {
        return false;
      }
    }
    return true;
  }

  void setCheckAll(CartList cartList, bool check) {
    for (Store store in cartList.carts) {
      for (Cart cart in store.cartList) {
        _checkMap[cart.cartId] = check;
      }
    }
    notifyListeners();
  }

  bool getCheckStore(Store store) {
    for (Cart cart in store.cartList) {
      if (!getCheckCart(cart)) {
        return false;
      }
    }
    return true;
  }

  void setCheckStore(Store store, bool check) {
    store.cartList.forEach((cart) {
      _checkMap[cart.cartId] = check;
    });
    notifyListeners();
  }

  bool getCheckCart(Cart cart) {
    return _checkMap[cart.cartId] ?? false;
  }

  void setCheckCart(Cart cart, bool check) {
    _checkMap[cart.cartId] = check;
    notifyListeners();
  }

  /// 合计人民币
  double totalRmb(CartList cartList) {
    double rmb = 0;
    for (Store store in cartList.carts) {
      for (Cart cart in store.cartList) {
        if (getCheckCart(cart)) {
          for (Price price in cart.priceVos) {
            if (price.coinType == 'RMB') {
              rmb += price.pointCoin * cart.quantity;
            }
          }
        }
      }
    }
    return rmb;
  }

  /// 合计积分币
  double totalJfb(CartList cartList) {
    double jfb = 0;
    for (Store store in cartList.carts) {
      for (Cart cart in store.cartList) {
        if (getCheckCart(cart)) {
          for (Price price in cart.priceVos) {
            if (price.coinType == 'JFB') {
              jfb += price.pointCoin * cart.quantity;
            }
          }
        }
      }
    }
    return jfb;
  }

  /// 合计数量
  int totalQuantity(CartList cartList) {
    int quantity = 0;
    for (Store store in cartList.carts) {
      for (Cart cart in store.cartList) {
        if (getCheckCart(cart)) {
          quantity += cart.quantity;
        }
      }
    }
    return quantity;
  }

  /// 检查能否下单
  Future<ViewState> orderCheck(CartList cartList) async {
    viewState = ViewState.busy;
    try {
      _cartIdQuantity = _totalCartIdQuantity(cartList);
      if (_cartIdQuantity.isEmpty) {
        throw '请选择商品';
      }
      await api.orderCheck(_cartIdQuantity);
      viewState = ViewState.success;
    } catch (e) {
      _errorMessage = e is DioError ? e.message : e.toString();
      viewState = ViewState.error;
    }
    return viewState;
  }

  /// Map<购物车编号, 数量>
  Map<String, int> _totalCartIdQuantity(CartList cartList) {
    Map<String, int> map = {};
    for (Store store in cartList.carts) {
      for (Cart cart in store.cartList) {
        if (getCheckCart(cart)) {
          map['${cart.cartId}'] = cart.quantity;
        }
      }
    }
    return map;
  }
}
